package GC;

import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;

import java.nio.ByteBuffer;
import java.util.*;

public class Test3 {
    public static void main(String[] args) {
        for (int i = 0; i < 1024; i++) {
            ByteBuffer.allocateDirect(1024*1024*10);
            System.out.println(i);
            System.gc();
        }
//        for (int i = 0; i < 10000; i++) {
//            new Thread(new SleepThread(), "Thread" + i).start();
//            System.out.println("Thread" + i + " created");
//        }
    }

    public static class SleepThread implements Runnable{

        @Override
        public void run() {
            try {
                Thread.sleep(100000000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

//        for (int i = 0; i < 1000000; i++) {
//            CglibBean bean = new CglibBean("geym.jvm.ch3.perm.bean"+i,new HashMap());
//        }

class CglibBean {
    /**
     * 实体Object
     */
    public Object object = null;


    /**
     * 属性map
     */
    public BeanMap beanMap = null;


    public CglibBean() {
        super();
    }


    @SuppressWarnings("unchecked")
    public CglibBean(String name, Map propertyMap) {
        this.object = generateBean(name, propertyMap);
        this.beanMap = BeanMap.create(this.object);
    }


    /**
     * 给bean属性赋值
     *
     * @param property 属性名
     * @param value    值
     */
    public void setValue(String property, Object value) {
        beanMap.put(property, value);
    }


    /**
     * 通过属性名得到属性值
     *
     * @param property 属性名
     * @return 值
     */
    public Object getValue(String property) {
        return beanMap.get(property);
    }


    /**
     * 得到该实体bean对象
     *
     * @return
     */
    public Object getObject() {
        return this.object;
    }


    @SuppressWarnings("unchecked")
    private Object generateBean(String name, Map propertyMap) {
        BeanGenerator generator = new BeanGenerator();
        Set keySet = propertyMap.keySet();
        for (Iterator i = keySet.iterator(); i.hasNext(); ) {
            String key = name + (String) i.next();
            generator.addProperty(key, (Class) propertyMap.get(key));
        }
        return generator.create();
    }
}

//    ArrayList<byte[]> list = new ArrayList<>();
//        for (int i = 0; i < 1024*10; i++) {
//        list.add(new byte[1024*1024]);
//        }